﻿using Project.Api;
using Project.API.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Project.BLL;
using Project.Model;

namespace Project.API.Controllers
{
    [ApiAuthorize]
    [RoutePrefix("api/menus")]
    public class MenusController : ApiController
    {
        // GET: api/Menus
        [HttpGet]
        [Route("")]
        public List<MenusDto> GetMenus() {
            AuthorityBll bll = new AuthorityBll();
            var usersinfo = Request.GetRouteData().Values["auth"] as AuthInfo;
            var data = bll.Query().Where(x => x.roleID == usersinfo.RolesID);
            var list = from d in data
                       select new MenusDto
                       {
                           menuID = d.menuID,
                           menuName = d.Menus.menuName,
                           url = d.Menus.url,
                           icon = d.Menus.icon,
                           lastTime = d.Menus.lastTime,
                           parentID = d.Menus.parentID
                       };
            return list.ToList();
        }
        [HttpGet]
        [Route("shows")]
        public List<MenusDto> GetMenu()
        {
            MenusBll bll = new MenusBll();
            var data = bll.Query();
            var list = from d in data
                       select new MenusDto
                       {
                           menuID = d.menuID,
                           menuName = d.menuName,
                           url = d.url,
                           icon = d.icon,
                           lastTime = d.lastTime,
                           parentID = d.parentID
                       };
            return list.ToList();
        }
        //添加菜单
        [HttpPost]
        [Route("")]
        public Result AddMenus(Menus menus) {
            Menus menu = new Menus();
            menu.menuName = menus.menuName;
            menu.url = menus.url;
            menu.parentID = menus.parentID;
            menu.icon = menus.icon;
            menu.lastTime = DateTime.Now;
            bool bol = new MenusBll().Add(menu);
            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "添加成功!" : "添加失败!";
            return result;
        }
        //删除菜单
        [HttpDelete]
        [Route("{mid:int}")]
        public Result RemoveMenus(int mid) {
            var list = new MenusBll().Query().Where(x => x.menuID == mid).FirstOrDefault();
            bool bol = new MenusBll().Delete(list);
            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "删除成功!!" : "error";
            return result;
        }
        //修改菜单
        [HttpPut]
        [Route("")]
        public Result EditMenus(Menus menus) {
            bool bol = new MenusBll().Update(menus);
            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "修改成功!" : "err";
            return result;
        }
    }
}
